package edu.pku.sei.modeler.editpolicies;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.ReconnectRequest;

import edu.pku.sei.modeler.model.core.MetaElement;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.Dependencies.Dependency;
import edu.pku.sei.modeler.model.uml.InternalStructures.Connector;
import edu.pku.sei.modeler.model.uml.Kernel.Association;
import edu.pku.sei.modeler.model.uml.Kernel.Generalization;
import edu.pku.sei.modeler.model.uml.Kernel.PackageImport;
import edu.pku.sei.modeler.model.uml.Kernel.PackageMerge;
import edu.pku.sei.modeler.uml.commands.UMLConnectionCreateCommand;
import edu.pku.sei.modeler.uml.commands.umlclass.ClassDiagramConnectionCreateFactory;

public class ClassNodeEditPolicy extends UMLDiagramNodeEditPolicy {


	@Override
	protected Command getConnectionCreateCommand(CreateConnectionRequest request) {
		MetaLink link = (MetaLink) request.getNewObject();

		UMLConnectionCreateCommand command = ClassDiagramConnectionCreateFactory.INSTANCE
				.createCommand(link, (MetaNode) getHost().getModel());
		request.setStartCommand(command);
		return command;
	}

	protected Command getConnectionCompleteCommand(CreateConnectionRequest request) {
		
		MetaLink newObj = (MetaLink)request.getNewObject();
		MetaElement ele = newObj.getModelElement();
		
		if(ele instanceof Dependency) {
			UMLConnectionCreateCommand cmd = (UMLConnectionCreateCommand)request.getStartCommand();
			cmd.setTarget((MetaNode)getHost().getModel());
			return cmd;
		}else if(ele instanceof Association) {
			UMLConnectionCreateCommand cmd = (UMLConnectionCreateCommand)request.getStartCommand();
			cmd.setTarget((MetaNode)getHost().getModel());
			return cmd;
		}else if (ele instanceof Generalization){
			UMLConnectionCreateCommand cmd = (UMLConnectionCreateCommand)request.getStartCommand();
			cmd.setTarget((MetaNode)getHost().getModel());
			return cmd;
		}else if (ele instanceof PackageImport){
			UMLConnectionCreateCommand cmd = (UMLConnectionCreateCommand)request.getStartCommand();
			cmd.setTarget((MetaNode)getHost().getModel());
			return cmd;
		}else if (ele instanceof PackageMerge){
			UMLConnectionCreateCommand cmd = (UMLConnectionCreateCommand)request.getStartCommand();
			cmd.setTarget((MetaNode)getHost().getModel());
			return cmd;
		}else if (ele instanceof Connector){
			UMLConnectionCreateCommand cmd = (UMLConnectionCreateCommand)request.getStartCommand();
			cmd.setTarget((MetaNode)getHost().getModel());
			return cmd;
		}
					
		return null;
	}
	
	protected Command getReconnectSourceCommand(ReconnectRequest request) {
		return UnexecutableCommand.INSTANCE;
	}
	protected Command getReconnectTargetCommand(ReconnectRequest request) {
		return UnexecutableCommand.INSTANCE;
	}

}
